package com.example.chatchatbackend.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chatchatbackend.entity.RestBean;
import com.example.chatchatbackend.entity.dto.Adv;
import com.example.chatchatbackend.entity.vo.request.AdvVO;
import com.example.chatchatbackend.service.AdvService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Slf4j
@RestController
@RequestMapping("api/adv")
public class AdvController {


    @Autowired
    private AdvService advService;

    @GetMapping
    public RestBean<List<Adv>> getAllAdv(){
        return RestBean.success(advService.getAllAdv());
    }

    @PostMapping
    public RestBean<AdvVO> insertAdv(@RequestBody AdvVO advVO){
        AdvVO insertAdvVO = advService.insertAdv(advVO);
        log.info("insert--Adv:{}",insertAdvVO);
        return RestBean.success(insertAdvVO);

    }

    @PostMapping("/list")
    public RestBean<List<Adv>> insertAdvList (@RequestBody List<Adv> advs){
        List<Adv> advList = advService.insertAdvList(advs);
        log.info("insert--AdvList:{}",advList);
        return RestBean.success(advList);
    }

    @GetMapping("/pageThree")
    public RestBean<IPage<Adv>> pageThree(@RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return RestBean.success(advService.pageThree(pageNum, pageSize));
    }
    
    @PutMapping
    public RestBean<AdvVO> updateAdv(@RequestBody AdvVO advVO){
        return RestBean.success(advService.updateAdv(advVO));

    }

    @DeleteMapping("/ids")
    public RestBean<String> deleteByIds(@RequestBody List<Integer> ids) {
        advService.deleteByIds(ids);
        return RestBean.success("删除成功");
    }

    @GetMapping("/ids")
    public RestBean<List<AdvVO>> findByIds(@RequestBody List<Integer> ids){
        List<AdvVO> advVOList = advService.findByIds(ids);
        return RestBean.success(advVOList);
    }

    @GetMapping("/title")
    public RestBean<List<AdvVO>> findByTitle(@RequestBody String title ){
        List<AdvVO> advVOList = advService.findByTitle(title);
        return RestBean.success(advVOList);
    }

}
